Skip to content

defer GC work while in cabi_realloc - #13

Merged
dicej merged 1 commit into
bytecodealliance:mainfrom
ricochet:cabi-realloc-no-gc
Oct 29, 2026
Merged

dicej merged 1 commit into
bytecodealliance:mainfrom
ricochet:cabi-realloc-no-gc

Conversation

@ricochet

@ricochet ricochet commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

This change defers GC work while in cabi_realloc. I ran into the same issue as bytecodealliance/componentize-go#77. This change fixes the issue without patching the adapter. Although I think now that we've already made the workarounds, it's still worth landing bytecodealliance/wasmtime#14319. These can be complementary.

Allocations made from cabi_realloc may start or finish a GC cycle, and the Go GC calls might call imports which trap. The monotonic clock is already paused via the adapter, but there are two remaining traps with cannot leave component instance:

  • gcMarkTermination reads the realtime clock (walltime -> wall-clock.now)
  • gcStart's startTheWorldWithSema polls timers (netpoll -> poll_oneoff)

The timer poll happens when a cycle starts:

cabi_realloc → Allocate → mallocgc → gcAssistAlloc → gcMarkDone → gcMarkTermination
  → time.now → walltime1 → clock_time_get(CLOCKID_REALTIME) → wasi:clocks/wall-clock.now   ⇒ trap

cabi_realloc → Allocate → mallocgc → gcStart → startTheWorldWithSema
  → netpoll → poll_oneoff → wasi:io/poll                                                   ⇒ trap

This shows up when a host lowers large results into the guest, e.g. a list of ~40k records (~80k consecutive cabi_realloc calls).

cabiRealloc now calls runtime.procPin() before allocating and runtime.procUnpin() after. With the M's lock count raised, the runtime neither starts a GC cycle nor performs assist work, so any GC work is deferred to the next allocation made outside cabi_realloc. This also keeps the GC from switching to other goroutines here. The allocations are still charged as assist debt, so the pacer catches up then.

This covers both traps above as well as any other import a GC cycle could reach from inside cabi_realloc, instead of patching the adapter one import at a time. For example, an assist that parks could let another goroutine run and call an import.

The runtime explicitly allows linking to procPin/procUnpin and commits to keeping their signatures (golang/go#67401). They are push linknames, so this doesn't depend on -checklinkname=0.

Follow-up to bytecodealliance/componentize-go#56. Related: bytecodealliance/componentize-go#77, bytecodealliance/wasmtime#14318.

Allocations made from `cabi_realloc` may start or finish a GC cycle, and
the Go GC calls imports while doing so, which the component model
forbids. The monotonic clock is already paused via the adapter, but
there are two remaining traps with `cannot leave component instance`:

- gcMarkTermination reads the realtime clock (walltime -> wall-clock.now)
- gcStart's startTheWorldWithSema polls timers (netpoll -> poll_oneoff)

This shows up when a host lowers large results into the guest, e.g. a
list of ~40k records (~80k consecutive `cabi_realloc` calls).

Wrap the allocation in `runtime.procPin`/`procUnpin`. With the M's lock
count raised, the runtime neither starts a GC cycle nor performs assist
work, so any GC work is deferred to the next allocation made outside
`cabi_realloc`. This also keeps the GC from switching to other
goroutines here. The runtime explicitly supports linking to these
functions (golang/go#67401).

See bytecodealliance/componentize-go#77 and bytecodealliance/wasmtime#14318.

Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
@dicej

dicej commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Thanks for this, @ricochet! Seems like a promising approach.

any GC work is deferred to the next allocation made outside cabi_realloc

I wonder if we could trigger a GC next time the guest is entered following a cabi_realloc which would have caused one, without necessarily waiting for an allocation to happen. I'm thinking of a scenario where the only allocations are cabi_realloc ones for an extended period of time, in which case we might want to GC before the next non-cabi_realloc happens.

Comment thread wit/runtime/runtime.go Resolved
@dicej

dicej commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

@ricochet are you planning to come back to this? I do think this is a promising approach, and I'm happy to take over this PR if you don't have time for it.

@ricochet

ricochet commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@ricochet are you planning to come back to this? I do think this is a promising approach, and I'm happy to take over this PR if you don't have time for it.

If you're able to take it over the line, please feel free!

@dicej dicej left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ricochet!

@dicej
dicej merged commit f4d21ac into bytecodealliance:main Oct 29, 2026
1 check passed
asteurer pushed a commit to bytecodealliance/componentize-go that referenced this pull request Oct 30, 2026
chenyan2002 pushed a commit to chenyan2002/wit-bindgen that referenced this pull request Oct 31, 2026
This pulls in bytecodealliance/go-pkg#13, fixing
bytecodealliance/wasmtime#14318 (which, despite being
entered as a Wasmtime issue, ended up being fixed in `go-pkg`).
ricochet added a commit to wasmCloud/go that referenced this pull request Nov 10, 2026
go.bytecodealliance.org/pkg v0.4.0 lets the Go garbage collector start a
cycle inside cabi_realloc. A GC cycle calls imports (clocks, I/O polling),
which the component model forbids there, so the component traps. It shows
up once a single value is big enough to trigger GC: a list<record> of ~40k
records traps while 20k works, and reading one 8.8 MiB NATS KV value traps
with gcStart and cabi_realloc in the backtrace. v0.4.1
(bytecodealliance/go-pkg#13) pins the M while allocating, deferring GC
work until the allocation returns.

- component/ and plugin/ require v0.4.1.
- Every example and the template pin v0.4.1 too. They build against the
  released SDK v0.1.6, which still names v0.4.0, so MVS needs the pin to
  pick the fix; and the air-gapped image seeds GOMODCACHE from these
  modules alone, so without it the in-repo SDK build would go offline
  short of v0.4.1.
- airgap/versions.env and BUILDING.md record the new pin and why it is
  the floor; check-versions.sh now fails if any module names another
  go-pkg version.

No binding regeneration: generated code names go-pkg by import path only.
componentize-go stays at v0.4.3.

Signed-off-by: Bailey Hayes <bailey@cosmonic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants